var BaseLogin = app.controller('BaseLogin', function($rootScope, $scope, $http,$state,$timeout,$cordovaDevice,$cordovaChat,BaseLoginData) {
	try{
		try{
			Wechat.isInstalled({scope: 'snsapi_userinfo'}, 
					function onSuccess(suc) {
				$rootScope.inswx=true;
			},
			function onError(err) {
				$rootScope.inswx=false;
			}
			);

			YCQQ.checkClientInstalled(
					function onSuccess(suc) {
						$rootScope.insqq=true;
					},
					function onError(err) {
						$rootScope.insqq=false;
					}
			);
		}catch (e) {
		}
		
		$rootScope.showf=false;
		$rootScope.title = '用户登录';
		$scope.show=false;
		$scope.uname="";
		$scope.pwd="";

		var reg = /(^13\d{9}$)|(^14)[5,7]\d{8}$|(^15[0,1,2,3,5,6,7,8,9]\d{8}$)|(^17)[6,7,8]\d{8}$|(^18\d{9}$)/g ;  
		if(reg.test(window.localStorage.u)){  
			$scope.uname=window.localStorage.u;
		}

		if($rootScope.ftel!=undefined&&$rootScope.ftel!=null&&$rootScope.ftel!=""){
			$scope.uname=$rootScope.ftel;

			$rootScope.ftel="";
		}

		$scope.keyPress=function(e){
			console.log(e);
			if(e.which==13){
				$scope.login();
			}
		}
		$scope.keyPressNext=function(e){
			console.log(e.which);
			if(e.which==13||e.which==9){
				$scope.login();
			}
		}

		//QQ登录
		$scope.qqlogintimer = null;
		$scope.qqlogin=function(){
			if(!$rootScope.insqq){
				showcatch("您还没有安装QQ");
			}
			ProgressIndicator.showSimpleWithLabel(false, '正在启动QQ...');
			$scope.qqlogintimer = window.setTimeout("ProgressIndicator.hide()", 10000);
			var checkClientIsInstalled = 1;//default is 0,only for iOS
			YCQQ.ssoLogin(function(args){
				/*
				 *
				 Object
				 access_token: "8D31F3B30ED7638006E4E026F828605B"
				 userid: "A1F5CBF8EBB5CDEA76300D135EE446DA"
				 * */
				console.log(args);
				if ($scope.qqlogintimer != null)
					window.clearTimeout($scope.qqlogintimer);
				ProgressIndicator.hide();
				ProgressIndicator.showSimpleWithLabel(false, '正在获取QQ登录信息...');

				var url = SITE_URL + "index.php?app=userapp&act=qqregister&callback=JSON_CALLBACK&t=" + args.access_token+"&id="+args.userid;
				$http.jsonp(url).success(function (data) {
					if (data.done) {

						//统计绑定注册用户
						razor('user','BaseLogin',{'uid':data.retval.user_id});

						var requestData = {};
						requestData.username = data.retval.user_id;
						requestData.password = '123456';
						requestData.nickname = data.retval.real_name;

						$.ajax({
							url: 'https://a1.easemob.com/darven02/dscj/users',
							type: 'POST',
							data: JSON.stringify(requestData),
							headers: {
								'Content-Type': 'application/json'
							},
							success: function (respData) {
								console.log(respData);

							},
							error: function (respData) {
								console.log(respData);
							},
						});

						window.localStorage.u = data.retval.user_name;
						window.localStorage.p = data.retval.password;


						try {
							easemobchat.login(
									{username: data.retval.user_id, password: '123456'},
									function (s) {
									},
									function (e) {
										console.log(e);
									}
							);
						} catch (e) {
							showcatch(e);
						}

						url = SITE_URL + "index.php?app=userapp&act=checkuser&callback=JSON_CALLBACK&u=" + window.localStorage.u + "&p=" + window.localStorage.p;
						$http.jsonp(url).success(function (data) {
							if (data.done) {
								ProgressIndicator.hide();
								var d = data.retval;
								window.localStorage.user = JSON.stringify(d);
								$rootScope.go("index");

								$cordovaChat.init(d.BOSH_SERVICE, d.CHATURI, d.CHATUSER, d.CHATUSERP);
								$cordovaChat.ChatConnect();
							}
							else {
								ProgressIndicator.hide();
								console.log("QQ登录失败，请重试1");
							}
						}).error(function () {
							ProgressIndicator.hide();
							console.log("QQ登录失败，请重试2");
						});
					}
					else {
						ProgressIndicator.hide();
						console.log("QQ登录失败，请重试4");
					}
				}).error(function () {
					ProgressIndicator.hide();
					console.log("QQ登录失败，请重试5");
				});
				console.log("qqlogin suc", suc);

			},function(failReason){
				console.log(failReason);
				if ($scope.qqlogintimer != null)
					window.clearTimeout($scope.qqlogintimer);
				ProgressIndicator.hide();
				console.log("登录失败" + failReason);
			},checkClientIsInstalled);
		};
		//登录方法
		$scope.login=function(){

			if($scope.uname==""){
				showtoast("请填写用户名");
				//showdialog("请填写用户名");
				return false;
			}
			if($scope.pwd==""){
				showtoast("请填写用户密码");
				//showdialog("请填写用户密码");
				return false;
			}
			try{
				$scope.model = $cordovaDevice.getModel();
				$scope.platform = $cordovaDevice.getPlatform();
				$scope.uuid = $cordovaDevice.getUUID();
			}catch(e){
				$scope.model = '';
				$scope.platform = '';
				$scope.uuid = '';
			}

			var url=SITE_URL+"index.php?app=userapp&act=login&uname="+$scope.uname+"&pwd="+$scope.pwd+"&pushid="+$rootScope.pushid+"&platform="+$scope.platform+"&model="+$scope.model+"&uuid="+$scope.uuid+"&callback=JSON_CALLBACK";
			$http.jsonp(url).success(function(data) {
				if(data.done){
					window.localStorage.u=$scope.uname;
					window.localStorage.p=$scope.pwd;

					url=SITE_URL+"index.php?app=userapp&act=checkuser&callback=JSON_CALLBACK&u="+$scope.uname+"&p="+$scope.pwd;
					$http.jsonp(url).success( function(data) {
						if(data.done){
							var d=data.retval;
							window.localStorage.user=JSON.stringify(d);
							var requestData={};
							requestData.username = d.CHATUSER;
							requestData.password = '123456';
							requestData.nickname = d.n;
							console.log(requestData);
							try{

								$.ajax({
									url: 'https://a1.easemob.com/darven02/dscj/users',
									type: 'POST',
									data: JSON.stringify(requestData),
									headers: {
										'Content-Type': 'application/json'
									},
									success: function (respData) {
										console.log(respData);

									},
									error: function (respData) {
										console.log(respData);
									},
								});


								easemobchat.login(
										{username:d.CHATUSER,password:'123456'},
										function(s){},
										function(e){console.log(e);}
								);
							}catch(e){showcatch(e);}

							if(window.localStorage.user!=undefined&&window.localStorage.user!=null&&window.localStorage.user!=""){
								var d=JSON.parse(window.localStorage.user);
								$cordovaChat.init(d.BOSH_SERVICE,d.CHATURI,d.CHATUSER,d.CHATUSERP);
								$cordovaChat.ChatConnect();
							}

							$rootScope.go("index");
						}
					});

				}else{
					//showtoast(data.msg);
					showdialog("登录失败，请检查您输入的手机号或密码是否正确");
					return false;
				}
			});
		}

		$scope.find=function(){
			$rootScope.ftel=$scope.uname;
			$rootScope.go('findpwd');
		}
	}
	catch (e) {
		showcatch(e);
	}
});

BaseLogin.BaseLoginData=function($q,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u!=undefined&&window.localStorage.u!=null&&window.localStorage.u!=""&&window.localStorage.p!=undefined&&window.localStorage.p!=null&&window.localStorage.p!=""){			
			$rootScope.go("index");
		}else{
			def.resolve(data);
		}

		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}